package com.infomanage.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.Person;
import com.infomanage.po.TechExchange;
import com.infomanage.vo.TechExchangeItem;

public class TechExchangeDAO extends BaseDAO<TechExchange>{
	public TechExchangeItem find(int id) throws DaoException{
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("techExchangeId");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] { Person.class, Person.class, Person.class, Person.class, Person.class, Person.class, Department.class },
					TechExchange.class, new String[] {"personId", "personId", "personId", "personId", "personId", "personId", "deptId" },
					new String[] {"deliXdzId", "deliZdzId", "deliOndutyPerson", "acceptXdzId", "acceptZdzId", "acceptOndutyPerson", "deptId"}, searchBean);
			List<TechExchangeItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	@SuppressWarnings("unchecked")
	public List<TechExchangeItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] { Person.class, Person.class, Person.class, Person.class, Person.class, Person.class, Department.class },
					TechExchange.class, new String[] {"personId", "personId", "personId", "personId", "personId", "personId", "deptId" },
					new String[] {"deliXdzId", "deliZdzId", "deliOndutyPerson", "acceptXdzId", "acceptZdzId", "acceptOndutyPerson", "deptId"});
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<TechExchangeItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] { Person.class, Person.class, Person.class, Person.class, Person.class, Person.class, Department.class },
				TechExchange.class, new String[] {"personId", "personId", "personId", "personId", "personId", "personId", "deptId" },
				new String[] {"deliXdzId", "deliZdzId", "deliOndutyPerson", "acceptXdzId", "acceptZdzId", "acceptOndutyPerson", "deptId"}, searchBean);
		return parseResult(result);
	}
	
	public List<TechExchangeItem> parseResult(List result){
		Iterator it = result.iterator();
		List<TechExchangeItem> list = new ArrayList<TechExchangeItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			TechExchange bean = (TechExchange) o[0];
			Person deliXdz = (Person) o[1];
			Person deliZdz = (Person) o[2];
			Person deliOndutyPerson = (Person) o[3];
			Person acceptXdz = (Person) o[4];
			Person acceptZdz = (Person) o[5];
			Person acceptOndutyPerson = (Person) o[6];
			TechExchangeItem item = new TechExchangeItem();
			item.setBean(bean);
			item.setDeliXdz(deliXdz);
			item.setDeliZdz(deliZdz);
			item.setDeliOndutyPerson(deliOndutyPerson);
			item.setAcceptXdz(acceptXdz);
			item.setAcceptZdz(acceptZdz);
			item.setAcceptOndutyPerson(acceptOndutyPerson);
			list.add(item);
		}
		return list;
	}

	public int count(SearchBean searchBean){
		return super.count(new Class[] { Person.class, Person.class, Person.class, Person.class, Person.class, Person.class, Department.class },
				TechExchange.class, new String[] {"personId", "personId", "personId", "personId", "personId", "personId", "deptId" },
				new String[] {"deliXdzId", "deliZdzId", "deliOndutyPerson", "acceptXdzId", "acceptZdzId", "acceptOndutyPerson", "deptId"}, searchBean);
	}
}
